﻿using System;
using System.Collections.Generic;
using Castle.ActiveRecord;
using Pampa.CRUD.Core;

namespace Demo.Model
{
    [ActiveRecord]
    public class Customer : AbstractActiveRecord<Customer>, IIdentifiable
    {
        private int id;
        private bool active; 
        private string firstName;
        private string lastName;
        private DateTime? birthdate;
        private CustomerType type;

        private Country nationality;
        private Address address;
        private IList<CustomerPhone> phones;
        private IList<Hobby> hobbies;
        private IList<Preference> preferences; 

        private string email;

        private DateTime? lastModification;
        private string observations;
        
        private int limit;
        private string user;

        public Customer()
        {
            phones = new List<CustomerPhone>();
            hobbies = new List<Hobby>();
            address = new Address();
        }
        
        [PrimaryKey]
        public virtual int ID
        {
            get { return id; }
            set { id = value; }
        }

        [Property]
        public virtual bool Active
        {
            get { return active; }
            set { active = value; }
        }

        [Property]
        public virtual string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        [Property]
        public virtual string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
        
        [BelongsTo("CountryID")]
        public virtual Country Nationality
        {
            get { return nationality; }
            set { nationality = value; }
        }

        [BelongsTo("AddressID", Cascade = CascadeEnum.All)]
        public virtual Address Address
        {
            get { return address; }
            set { address = value; }
        }

        [HasMany(typeof(CustomerPhone)
            , Inverse = true
            , Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
        public virtual IList<CustomerPhone> Phones
        {
            get { return phones; }
            set { phones = value; }
        }

        [HasAndBelongsToMany(typeof(Hobby)
            , Table = "CustomerHobby"
            , ColumnKey = "CustomerID"
            , ColumnRef = "HobbyID"
            , Cascade = ManyRelationCascadeEnum.All)]
        public IList<Hobby> Hobbies
        {
            get { return hobbies; }
            set { hobbies = value; }
        }

        [HasAndBelongsToMany(typeof(Preference)
            , Table = "CustomerPreference"
            , ColumnKey = "CustomerID"
            , ColumnRef = "PreferenceID"
            , Cascade = ManyRelationCascadeEnum.All)]
        public IList<Preference> Preferences
        {
            get { return preferences; }
            set { preferences = value; }
        } 

        [Property]
        public virtual string Email
        {
            get { return email; }
            set { email = value; }
        }

        [Property]
        public virtual DateTime? BirthDate
        {
            get { return birthdate; }
            set { birthdate = value; }
        }

        [Property]
        public virtual int Limit
        {
            get { return limit; }
            set { limit = value; }
        }

        [BelongsTo("TypeID")]
        public virtual CustomerType Type
        {
            get { return type; }
            set { type = value; }
        }

        [Property]
        public virtual DateTime? LastModification
        {
            get { return lastModification; }
            set { lastModification = value; }
        }

        [Property("[User]")]
        public virtual string User
        {
            get { return user; }
            set { user = value; }
        }

        [Property]
        public virtual string Observations
        {
            get { return observations; }
            set { observations = value;  }
        }

        [Property]
        public virtual string Description
        {
            get { return LastName + ", " + FirstName; }
            set { ; }
        }

        public string Id
        {
            get { return this.ID.ToString(); }
        }

        public int ParseId(string strId)
        {
            return int.Parse(strId);
        }

        public override void Save()
        {
            // Actualizo referencias cruzadas
            foreach (CustomerPhone phone in this.Phones)
            {
                phone.Customer = this;
            }

            address.Customer = this;
            
            base.Save();
        }
    }
}
